﻿// Problem 36
// 
//    The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.
//
//    Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
//
//    (Please note that the palindromic number, in either base, may not include leading zeros.)
//
// correct answer is 872187

#light

let isPalindrome (nBase:int) (n:int) =
    let rec endsMatch (str:string) n len = 
        if n > len / 2 then true
        elif str.[n-1] = str.[len-n] then endsMatch str (n+1) len
        else false
    let s = System.Convert.ToString(n, nBase)
    endsMatch s 1 s.Length

let isBinaryDecimalPalindrome n = isPalindrome 10 n && isPalindrome 2 n
            
let palindromeListSum max = 
    [1..max-1] 
    |> Seq.filter isBinaryDecimalPalindrome 
    |> Seq.sum

let solve() = palindromeListSum 1000000